
import {deptMap, teacherMap, studentMap, createData, checkList, readDept, readTeacher, readStudent, 
  setStartYearList, setRelationList, setStageList, setSubjectList, setStuGradeList, setGradeList} from '@/util/map'

import xlsx from '@/util/xlsx'
import uuid from '@/util/uuid'
import {importDept, getDept} from '@/api/deptApi'
import {importTeacher, getSubject} from '@/api/teacherApi'
import {importStudent} from '@/api/studentListApi'
import {importTerm, putSchool, getSchool, dictionaryvalue, getPeroidsList, getSchoolStage, getGradeList, gradesBySchoolId} from '@/api/schoolApi'
import router from '@/router'
const initSchool = {
    namespaced: true,
    state: {
      deptFileList: localStorage.deptFileList ? JSON.parse(localStorage.deptFileList) : [],
      deptList: localStorage.deptList ? JSON.parse(localStorage.deptList) : [],
      teacherFileList: localStorage.teacherFileList ? JSON.parse(localStorage.teacherFileList) : [],
      teacherList: localStorage.teacherList ? JSON.parse(localStorage.teacherList) : [],
      studentFileList: localStorage.studentFileList ? JSON.parse(localStorage.studentFileList) : [],
      studentList: localStorage.studentList ? JSON.parse(localStorage.studentList) : [],

      termList: localStorage.termList ? JSON.parse(localStorage.termList) : [],

      modifyVisible: false,
      loaddingVisible: false,
      type: '',
      action: '',

      initState: 0,

      peroidsList: []
    },
    getters: {
      deptWrongList (state) {
        var list = []
        for (var item of state.deptList) {
          var flag = true
          for (var key in item) {
            if (!item[key].legal && typeof item[key] == 'object') {
              flag = false
            }
          }
          if (!flag) {
            list.push(item)
          }
        }
        return list
      },
      teacherWrongList (state) {
        var list = []
        for (var item of state.teacherList) {
          var flag = true
          for (var key in item) {
            if (!item[key].legal && typeof item[key] == 'object') {
              flag = false
            }
          }
          if (!flag) {
            list.push(item)
          }
        }
        return list
      },
      studentWrongList (state) {
        var list = []
        for (var item of state.studentList) {
          var flag = true
          for (var key in item) {
            if (!item[key].legal && typeof item[key] == 'object') {
              flag = false
            }
          }
          if (!flag) {
            list.push(item)
          }
        }
        return list
      }
    },
    mutations: {
       pushDeptList (state, list) {
          state.deptList = state.deptList.concat(list)
          localStorage.deptList = JSON.stringify(state.deptList)
       },
       pushTeacherList (state, list) {
          state.teacherList = state.teacherList.concat(list)
          localStorage.teacherList = JSON.stringify(state.teacherList)
      },
      pushStudentList (state, list) {
          state.studentList = state.studentList.concat(list)
          localStorage.studentList = JSON.stringify(state.studentList)
      },
      pushDeptFile (state, obj) {
          state.deptFileList.push(obj)
          localStorage.deptFileList = JSON.stringify(state.deptFileList)
      },
      pushTeacherFile (state, obj) {
          state.teacherFileList.push(obj)
          localStorage.teacherFileList = JSON.stringify(state.teacherFileList)
      },
      pushStudentFile (state, obj) {
          state.studentFileList.push(obj)
          localStorage.studentFileList = JSON.stringify(state.studentFileList)
      },
      addTerm (state, term) {
        state.termList.push(term)
        localStorage.termList = JSON.stringify(state.termList)
      },
      deleteTerm (state, index) {
          state.termList.splice(index, 1)
          localStorage.termList = JSON.stringify(state.termList)
      },
      clear (state) {
          state.deptList = []
          state.teacherList = []
          state.studentList = []
          state.deptFileList = []
          state.teacherFileList = []
          state.studentFileList = []
          state.termList = []
          localStorage.removeItem('deptList')
          localStorage.removeItem('teacherList')
          localStorage.removeItem('studentList')
          localStorage.removeItem('deptFileList')
          localStorage.removeItem('teacherFileList')
          localStorage.removeItem('studentFileList')
          localStorage.removeItem('termList')
      }
    },
    actions: { 
        exportDeptTemplate () {
            var obj = {}
            for (var key in deptMap) {
              obj[key] = ''
            }
            xlsx.export([obj], '部门信息.xlsx')
        },
        exportTeacherTemplate () {
            var obj = {}
              for (var key in teacherMap) {
                obj[key] = ''
              }
              xlsx.export([obj], '教职工信息.xlsx')
        },
        exportStudentTemplate () {
            var obj = {}
            for (var key in studentMap) {
              obj[key] = ''
            }
            xlsx.export([obj], '学生信息.xlsx')
        },
        
        readDept ({state, commit, dispatch}, {file, fileId}) {
            return new Promise((resolve, reject) => {
              readDept({file, fileId, check: true}).then(list => {
                console.info('pushDeptList', list)
                if (list.length > 0) {
                  commit('pushDeptList', list)
                }
                resolve()
              })
            })
        },
        readTeacher ({state, commit, dispatch}, {file, fileId}) {
            return new Promise((resolve, reject) => {
                readTeacher({file, fileId, check: true}).then(list => {
                  console.info('pushTeacherList', list)
                  if (list.length > 0) {
                    commit('pushTeacherList', list)
                  }
                    resolve()
                })
            })
        },
        readStudent ({state, commit, dispatch}, {file, fileId}) {
          return new Promise((resolve, reject) => {
            readStudent({file, fileId, check: true}).then(list => {
              console.info('pushStudentList', list)
              if (list.length > 0) {
                commit('pushStudentList', list)
              }
                 
              resolve()
            })
          })
        },
      
        removeDeptFile ({state, commit, dispatch}, file) {
            for (let i = state.deptFileList.length - 1; i >= 0; i--) {
              let temp = state.deptFileList[i]
              if (temp.id == file.id) {
                state.deptFileList.splice(i, 1)
              }
            }
            localStorage.deptFileList = JSON.stringify(state.deptFileList)
            for (let i = state.deptList.length - 1; i >= 0; i--) {
              let temp = state.deptList[i]
              if (temp.fileId == file.id) {
                state.deptList.splice(i, 1)
              }
            }
            localStorage.deptList = JSON.stringify(state.deptList)
        },
        removeTeacherFile ({state, commit, dispatch}, file) {
          for (let i = state.teacherFileList.length - 1; i >= 0; i--) {
            let temp = state.teacherFileList[i]
            if (temp.id == file.id) {
              state.teacherFileList.splice(i, 1)
            }
          }
          localStorage.teacherFileList = JSON.stringify(state.teacherFileList)
          for (let i = state.teacherList.length - 1; i >= 0; i--) {
            let temp = state.teacherList[i]
            if (temp.fileId == file.id) {
              state.teacherList.splice(i, 1)
            }
          }
          localStorage.teacherList = JSON.stringify(state.teacherList)
        },
        removeStudentFile ({state, commit, dispatch}, file) {
            for (let i = state.studentFileList.length - 1; i >= 0; i--) {
              let temp = state.studentFileList[i]
              if (temp.id == file.id) {
                state.studentFileList.splice(i, 1)
              }
            }
            localStorage.studentFileList = JSON.stringify(state.studentFileList)
            for (let i = state.studentList.length - 1; i >= 0; i--) {
              let temp = state.studentList[i]
              if (temp.fileId == file.id) {
                state.studentList.splice(i, 1)
              }
            }
            localStorage.studentList = JSON.stringify(state.studentList)
        },
       
        deleteDeptById ({state, commit, dispatch}, id) {
          for (var i = 0; i < state.deptList.length; i++) {
              if (state.deptList[i].id == id) {
                state.deptList.splice(i, 1)
              }
          }
          localStorage.deptList = JSON.stringify(state.deptList)
        },

        deleteTeacherById ({state, commit, dispatch}, id) {
          for (var i = 0; i < state.teacherList.length; i++) {
              if (state.teacherList[i].id == id) {
                state.teacherList.splice(i, 1)
              }
          }
          localStorage.teacherList = JSON.stringify(state.teacherList)
        },
        deleteStudentById ({state, commit, dispatch}, id) {
          for (var i = 0; i < state.studentList.length; i++) {
              if (state.studentList[i].id == id) {
                state.studentList.splice(i, 1)
              }
          }
          localStorage.studentList = JSON.stringify(state.studentList)
        },
        modifyDept ({state, commit, dispatch}, list) {
          for (var i = 0; i < state.deptList.length; i++) {
              var item = state.deptList[i]
              for (var n = 0; n < list.length; n++) {
                  var obj = list[n]
                  if (item.id == obj.id) {
                     state.deptList.splice(i, 1, obj)
                  }
              }
          }
        
          state.deptList = checkList({list: state.deptList, map: deptMap})
          localStorage.deptList = JSON.stringify(state.deptList)
        },
        modifyTeacher ({state, commit, dispatch}, list) {
          for (var i = 0; i < state.teacherList.length; i++) {
              var item = state.teacherList[i]
              for (var n = 0; n < list.length; n++) {
                  var obj = list[n]
                  if (item.id == obj.id) {
                    state.teacherList.splice(i, 1, obj)
                  }
              }
          }
          state.teacherList = checkList({list: state.teacherList, map: teacherMap})
          localStorage.teacherList = JSON.stringify(state.teacherList)
        },
        modifyStudent ({state, commit, dispatch}, list) {
          for (var i = 0; i < state.studentList.length; i++) {
              var item = state.studentList[i]
              for (var n = 0; n < list.length; n++) {
                  var obj = list[n]
                  if (item.id == obj.id) {
                    state.studentList.splice(i, 1, obj)
                  }
              }
          }
          
          state.studentList = checkList({list: state.studentList, map: studentMap})
          localStorage.studentList = JSON.stringify(state.studentList)
        },
        async putSchool ({state, commit, dispatch}, parmas) {
             if (location.hash != '#/init') {
                return 
             }
             var res = await putSchool(parmas)
             state.initState = parmas.initState
             return res
        },
        async getSchool ({state, commit, dispatch}, parmas) {
            var res = await getSchool(parmas)
            state.initState = res.initState
        },
        handleErrorTeacher ({state, commit, dispatch}, list) {
            for (var item of list) {
              var col = ''
              for (var key in item) {
                  if (key != 'errormsg') {
                     col = key.replace(key[0], key[0].toLowerCase())
                     item[col] = item[key]
                  }
              }
             
              for (var i = 0; i < state.teacherList.length; i++) {
                var t = state.teacherList[i]
                if (t[col].val == item[col]) {
                    t[col].legal = false
                    t[col].errVal = item[col]
                    t[col].errDesc = item.errormsg
                    state.teacherList.splice(i, 1, t)
                }
              }
            }
            localStorage.teacherList = JSON.stringify(state.teacherList)
        },
        handleErrorStudent ({state, commit, dispatch}, list) {
          for (var item of list) {
            var col = ''
            for (var key in item) {
                if (key != 'errormsg') {
                   col = key.replace(key[0], key[0].toLowerCase())
                   item[col] = item[key]
                }
            }
          
            for (var i = 0; i < state.studentList.length; i++) {
              var t = state.studentList[i]
              if (t[col].val == item[col]) {
                  t[col].legal = false
                  t[col].errVal = item[col]
                  t[col].errDesc = item.errormsg
                  state.studentList.splice(i, 1, t)
              }
            }
          }
          localStorage.studentList = JSON.stringify(state.studentList)
      }
    }
}
export default initSchool
